﻿using System;
using System.ComponentModel;
using System.Drawing.Drawing2D;

namespace ExFlash.Records
{
    [TypeConverter(typeof(MatrixConverter))]
    public class MatrixRecord : ISwfSerializable
    {
        public Boolean HasScale
        {
            get;
            private set;
        }
        
        [Browsable(false)]
        public Byte ScaleBitsNumber
        {
            get;
            private set;
        }

        public float ScaleX
        {
            get;
            private set;
        }

        public float ScaleY
        {
            get;
            private set;
        }

        public Boolean HasRotate
        {
            get;
            private set;
        }

        [Browsable(false)]
        public Byte RotateBitsNumber
        {
            get;
            private set;
        }

        public float RotateSkew0
        {
            get;
            private set;
        }

        public float RotateSkew1
        {
            get;
            private set;
        }

        [Browsable(false)]
        public Byte TranslateBitsNumber
        {
            get;
            private set;
        }

        public int TranslateX
        {
            get;
            private set;
        }

        public int TranslateY
        {
            get;
            private set;
        }

        public static explicit operator Matrix(MatrixRecord rec)
        {
            return new Matrix(
                rec.ScaleX,
                rec.RotateSkew0,
                rec.RotateSkew1,
                rec.ScaleY,
                rec.TranslateX,
                rec.TranslateY);
        }

        #region ISwfSerializable Members

        public void SwfDeserialize(BinaryBitReader reader)
        {
            HasScale = reader.ReadBoolean();

            if (HasScale)
            {
                ScaleBitsNumber = (Byte)reader.ReadUInt32(5);
                ScaleX = reader.ReadFloat(ScaleBitsNumber);
                ScaleY = reader.ReadFloat(ScaleBitsNumber);
            }

            HasRotate = reader.ReadBoolean();

            if (HasRotate)
            {
                RotateBitsNumber = (Byte)reader.ReadUInt32(5);
                RotateSkew0 = reader.ReadFloat(RotateBitsNumber);
                RotateSkew1 = reader.ReadFloat(RotateBitsNumber);
            }

            TranslateBitsNumber = (Byte)reader.ReadUInt32(5);

            TranslateX = reader.ReadInt32(TranslateBitsNumber);

            TranslateY = reader.ReadInt32(TranslateBitsNumber);

            reader.AlignBits();
        }

        public void SwfSerialize(byte[] arr, ref int offset)
        {
            throw new NotImplementedException();
        }

        #endregion
    }

    public class MatrixConverter : ExpandableObjectConverter
    {

    }
}
